home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / python2.5 / lib-tk / FileDialog.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-29  |  10KB  |  283 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''File selection dialog classes.
  5.  
  6. Classes:
  7.  
  8. - FileDialog
  9. - LoadFileDialog
  10. - SaveFileDialog
  11.  
  12. '''
  13. from Tkinter import *
  14. from Dialog import Dialog
  15. import os
  16. import fnmatch
  17. dialogstates = { }
  18.  
  19. class FileDialog:
  20.     """Standard file selection dialog -- no checks on selected file.
  21.  
  22.     Usage:
  23.  
  24.         d = FileDialog(master)
  25.         fname = d.go(dir_or_file, pattern, default, key)
  26.         if fname is None: ...canceled...
  27.         else: ...open file...
  28.  
  29.     All arguments to go() are optional.
  30.  
  31.     The 'key' argument specifies a key in the global dictionary
  32.     'dialogstates', which keeps track of the values for the directory
  33.     and pattern arguments, overriding the values passed in (it does
  34.     not keep track of the default argument!).  If no key is specified,
  35.     the dialog keeps no memory of previous state.  Note that memory is
  36.     kept even when the dialog is canceled.  (All this emulates the
  37.     behavior of the Macintosh file selection dialogs.)
  38.  
  39.     """
  40.     title = 'File Selection Dialog'
  41.     
  42.     def __init__(self, master, title = None):
  43.         if title is None:
  44.             title = self.title
  45.         
  46.         self.master = master
  47.         self.directory = None
  48.         self.top = Toplevel(master)
  49.         self.top.title(title)
  50.         self.top.iconname(title)
  51.         self.botframe = Frame(self.top)
  52.         self.botframe.pack(side = BOTTOM, fill = X)
  53.         self.selection = Entry(self.top)
  54.         self.selection.pack(side = BOTTOM, fill = X)
  55.         self.selection.bind('<Return>', self.ok_event)
  56.         self.filter = Entry(self.top)
  57.         self.filter.pack(side = TOP, fill = X)
  58.         self.filter.bind('<Return>', self.filter_command)
  59.         self.midframe = Frame(self.top)
  60.         self.midframe.pack(expand = YES, fill = BOTH)
  61.         self.filesbar = Scrollbar(self.midframe)
  62.         self.filesbar.pack(side = RIGHT, fill = Y)
  63.         self.files = Listbox(self.midframe, exportselection = 0, yscrollcommand = (self.filesbar, 'set'))
  64.         self.files.pack(side = RIGHT, expand = YES, fill = BOTH)
  65.         btags = self.files.bindtags()
  66.         self.files.bindtags(btags[1:] + btags[:1])
  67.         self.files.bind('<ButtonRelease-1>', self.files_select_event)
  68.         self.files.bind('<Double-ButtonRelease-1>', self.files_double_event)
  69.         self.filesbar.config(command = (self.files, 'yview'))
  70.         self.dirsbar = Scrollbar(self.midframe)
  71.         self.dirsbar.pack(side = LEFT, fill = Y)
  72.         self.dirs = Listbox(self.midframe, exportselection = 0, yscrollcommand = (self.dirsbar, 'set'))
  73.         self.dirs.pack(side = LEFT, expand = YES, fill = BOTH)
  74.         self.dirsbar.config(command = (self.dirs, 'yview'))
  75.         btags = self.dirs.bindtags()
  76.         self.dirs.bindtags(btags[1:] + btags[:1])
  77.         self.dirs.bind('<ButtonRelease-1>', self.dirs_select_event)
  78.         self.dirs.bind('<Double-ButtonRelease-1>', self.dirs_double_event)
  79.         self.ok_button = Button(self.botframe, text = 'OK', command = self.ok_command)
  80.         self.ok_button.pack(side = LEFT)
  81.         self.filter_button = Button(self.botframe, text = 'Filter', command = self.filter_command)
  82.         self.filter_button.pack(side = LEFT, expand = YES)
  83.         self.cancel_button = Button(self.botframe, text = 'Cancel', command = self.cancel_command)
  84.         self.cancel_button.pack(side = RIGHT)
  85.         self.top.protocol('WM_DELETE_WINDOW', self.cancel_command)
  86.         self.top.bind('<Alt-w>', self.cancel_command)
  87.         self.top.bind('<Alt-W>', self.cancel_command)
  88.  
  89.     
  90.     def go(self, dir_or_file = os.curdir, pattern = '*', default = '', key = None):
  91.         if key and dialogstates.has_key(key):
  92.             (self.directory, pattern) = dialogstates[key]
  93.         else:
  94.             dir_or_file = os.path.expanduser(dir_or_file)
  95.             if os.path.isdir(dir_or_file):
  96.                 self.directory = dir_or_file
  97.             else:
  98.                 (self.directory, default) = os.path.split(dir_or_file)
  99.         self.set_filter(self.directory, pattern)
  100.         self.set_selection(default)
  101.         self.filter_command()
  102.         self.selection.focus_set()
  103.         self.top.wait_visibility()
  104.         self.top.grab_set()
  105.         self.how = None
  106.         self.master.mainloop()
  107.         if key:
  108.             (directory, pattern) = self.get_filter()
  109.             if self.how:
  110.                 directory = os.path.dirname(self.how)
  111.             
  112.             dialogstates[key] = (directory, pattern)
  113.         
  114.         self.top.destroy()
  115.         return self.how
  116.  
  117.     
  118.     def quit(self, how = None):
  119.         self.how = how
  120.         self.master.quit()
  121.  
  122.     
  123.     def dirs_double_event(self, event):
  124.         self.filter_command()
  125.  
  126.     
  127.     def dirs_select_event(self, event):
  128.         (dir, pat) = self.get_filter()
  129.         subdir = self.dirs.get('active')
  130.         dir = os.path.normpath(os.path.join(self.directory, subdir))
  131.         self.set_filter(dir, pat)
  132.  
  133.     
  134.     def files_double_event(self, event):
  135.         self.ok_command()
  136.  
  137.     
  138.     def files_select_event(self, event):
  139.         file = self.files.get('active')
  140.         self.set_selection(file)
  141.  
  142.     
  143.     def ok_event(self, event):
  144.         self.ok_command()
  145.  
  146.     
  147.     def ok_command(self):
  148.         self.quit(self.get_selection())
  149.  
  150.     
  151.     def filter_command(self, event = None):
  152.         (dir, pat) = self.get_filter()
  153.         
  154.         try:
  155.             names = os.listdir(dir)
  156.         except os.error:
  157.             self.master.bell()
  158.             return None
  159.  
  160.         self.directory = dir
  161.         self.set_filter(dir, pat)
  162.         names.sort()
  163.         subdirs = [
  164.             os.pardir]
  165.         matchingfiles = []
  166.         for name in names:
  167.             fullname = os.path.join(dir, name)
  168.             if os.path.isdir(fullname):
  169.                 subdirs.append(name)
  170.                 continue
  171.             if fnmatch.fnmatch(name, pat):
  172.                 matchingfiles.append(name)
  173.                 continue
  174.         
  175.         self.dirs.delete(0, END)
  176.         for name in subdirs:
  177.             self.dirs.insert(END, name)
  178.         
  179.         self.files.delete(0, END)
  180.         for name in matchingfiles:
  181.             self.files.insert(END, name)
  182.         
  183.         (head, tail) = os.path.split(self.get_selection())
  184.         if tail == os.curdir:
  185.             tail = ''
  186.         
  187.         self.set_selection(tail)
  188.  
  189.     
  190.     def get_filter(self):
  191.         filter = self.filter.get()
  192.         filter = os.path.expanduser(filter)
  193.         if filter[-1:] == os.sep or os.path.isdir(filter):
  194.             filter = os.path.join(filter, '*')
  195.         
  196.         return os.path.split(filter)
  197.  
  198.     
  199.     def get_selection(self):
  200.         file = self.selection.get()
  201.         file = os.path.expanduser(file)
  202.         return file
  203.  
  204.     
  205.     def cancel_command(self, event = None):
  206.         self.quit()
  207.  
  208.     
  209.     def set_filter(self, dir, pat):
  210.         if not os.path.isabs(dir):
  211.             
  212.             try:
  213.                 pwd = os.getcwd()
  214.             except os.error:
  215.                 pwd = None
  216.  
  217.             if pwd:
  218.                 dir = os.path.join(pwd, dir)
  219.                 dir = os.path.normpath(dir)
  220.             
  221.         
  222.         self.filter.delete(0, END)
  223.         None(self.filter.insert, END(os.path.join, os.curdir if not dir else '*'))
  224.  
  225.     
  226.     def set_selection(self, file):
  227.         self.selection.delete(0, END)
  228.         self.selection.insert(END, os.path.join(self.directory, file))
  229.  
  230.  
  231.  
  232. class LoadFileDialog(FileDialog):
  233.     '''File selection dialog which checks that the file exists.'''
  234.     title = 'Load File Selection Dialog'
  235.     
  236.     def ok_command(self):
  237.         file = self.get_selection()
  238.         if not os.path.isfile(file):
  239.             self.master.bell()
  240.         else:
  241.             self.quit(file)
  242.  
  243.  
  244.  
  245. class SaveFileDialog(FileDialog):
  246.     '''File selection dialog which checks that the file may be created.'''
  247.     title = 'Save File Selection Dialog'
  248.     
  249.     def ok_command(self):
  250.         file = self.get_selection()
  251.         if os.path.exists(file):
  252.             if os.path.isdir(file):
  253.                 self.master.bell()
  254.                 return None
  255.             
  256.             d = Dialog(self.top, title = 'Overwrite Existing File Question', text = 'Overwrite existing file %r?' % (file,), bitmap = 'questhead', default = 1, strings = ('Yes', 'Cancel'))
  257.             if d.num != 0:
  258.                 return None
  259.             
  260.         else:
  261.             (head, tail) = os.path.split(file)
  262.             if not os.path.isdir(head):
  263.                 self.master.bell()
  264.                 return None
  265.             
  266.         self.quit(file)
  267.  
  268.  
  269.  
  270. def test():
  271.     '''Simple test program.'''
  272.     root = Tk()
  273.     root.withdraw()
  274.     fd = LoadFileDialog(root)
  275.     loadfile = fd.go(key = 'test')
  276.     fd = SaveFileDialog(root)
  277.     savefile = fd.go(key = 'test')
  278.     print loadfile, savefile
  279.  
  280. if __name__ == '__main__':
  281.     test()
  282.  
  283.